<?php
/**
 * TinyCatPHP 极简、快速、创新、易用、高性能的PHP应用开发框架
 * User: CnTaiGe <hst042@163.com>
 * Date: 2017/11/1
 */

namespace TinyCatPHP\libs\config;

use TinyCatPHP\libs\handler\Response;

class Config
{
    protected $_configs;
    /**
     * @var Config
     */
    protected static $_instance = null;

    private function __construct()
    {
        include TINY_CAT_SYS_ROOT . '/common/constant.php';
        $this->_configs = Config::load('config');
    }

    /**
     * 初始化配置
     */
    public static function init()
    {
        if (Config::$_instance === null)
            Config::$_instance = new self();
        date_default_timezone_set(self::get('timezone'));
        Response::contentType('text/html');
        Response::header('X-Powered-By', 'TinyCatPHP');
        if (Config::get('session_enable')) {
            session_start();
        }
    }

    /**
     * 加载配置文件
     */
    public static function load($loadFile)
    {
        $data = [];
        $data1 = [];
        $file1 = APP_PATH . '/config/' . PROD_DEV_ENV . '/' . $loadFile . '.php';
        if (file_exists($file1))
            $data1 = include $file1;
        $file = APP_PATH . '/config/' . $loadFile . '.php';
        if (file_exists($file)) {
            $data = include $file;
        }
        $datas = array_merge(is_array($data) ? $data : [], is_array($data1) ? $data1 : []);
        return $datas;
    }


    public static function set($key, $value)
    {
        Config::$_instance->_configs[$key] = $value;
    }

    public static function del($key)
    {
        if (!isset(Config::$_instance->_configs[$key])) return null;
        unset(Config::$_instance->_configs[$key]);
    }

    public static function get($key)
    {
        if (!isset(Config::$_instance->_configs[$key])) return null;
        return Config::$_instance->_configs[$key];
    }
}